package com.castle.fortress.admin.flowable.delegate;

import cn.hutool.core.util.StrUtil;
import com.castle.fortress.admin.flowable.service.FlowableManageService;
import com.castle.fortress.admin.system.entity.SysUser;
import com.castle.fortress.common.enums.YesNoEnum;
import org.flowable.common.engine.api.delegate.Expression;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.delegate.TaskListener;
import org.flowable.task.service.delegate.DelegateTask;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * 用户任务监听器
 * @author castle
 */
@Service
public class FortressUserTaskCreateListener implements TaskListener {
    @Autowired
    private FlowableManageService flowableManageService;
    @Autowired
    private RuntimeService runtimeService;
    //无用户审批时的处理方式 TO_PASS/指定用户ID
    private Expression emptyType;

    @Override
    public void notify(DelegateTask delegateTask) {
        System.out.println("--------FortressUserTaskCreateListener--------");
        System.out.println("无用户审批时的处理方式："+emptyType.getExpressionText());
        String assignee = delegateTask.getAssignee();
        //审核人是-10000，则自动通过
        if(delegateTask.getAssignee()!=null&&delegateTask.getAssignee().equals("-10000")){
            String emptyExpress=emptyType.getExpressionText();
            //自动通过
            if("TO_PASS".equals(emptyExpress)){
                SysUser user=new SysUser();
                user.setId(-10000L);
                user.setLoginName("-10000");
                Map<String,String> params = new HashMap<>();
                params.put("tid",delegateTask.getVariable("tid").toString());
                params.put("id",delegateTask.getVariable("id").toString());
                params.put("task",delegateTask.getId());
                params.put("approveStatus", YesNoEnum.YES.getCode().toString());
                params.put("approveOpinion","审批通过");
                flowableManageService.completeTask(user,params);
            //指定用户
            }else{
                delegateTask.setAssignee(emptyExpress);
                assignee = emptyExpress;
            }
        }
        //将当前审理人放入参数
        if(StrUtil.isNotEmpty(assignee)){
            runtimeService.setVariable(delegateTask.getExecutionId(),assignee,assignee);
//            String assigns = CommonUtil.emptyIfNull(runtimeService.getVariable(delegateTask.getExecutionId(),"_castle_flowable_current_assignees"));
//            if(StrUtil.isEmpty(assigns)){
//                runtimeService.setVariable(delegateTask.getExecutionId(),"_castle_flowable_current_assignees",delegateTask.getTaskDefinitionKey()+","+assignee);
//            }else{
//                String[] datas = assigns.split(",");
//                if(delegateTask.getTaskDefinitionKey().equals(datas[0])){
//                    runtimeService.setVariable(delegateTask.getExecutionId(),"_castle_flowable_current_assignees",runtimeService.getVariable(delegateTask.getExecutionId(),"_castle_flowable_current_assignees")+","+assignee);
//                }else{
//                    runtimeService.setVariable(delegateTask.getExecutionId(),"_castle_flowable_current_assignees",delegateTask.getTaskDefinitionKey()+","+assignee);
//                }
//            }
//            System.out.println("当前节点审批人："+runtimeService.getVariable(delegateTask.getExecutionId(),"_castle_flowable_current_assignees"));
        }
    }
}
